package com.workcell.workdata.im.model.dto;


import com.farsunset.cim.model.Message;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.workcell.workdata.im.component.predicate.ChannelFilterPredicate;
import io.netty.channel.Channel;

import java.util.LinkedList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class BatchMessage {
    /**
     * 请求ID
     */
    private String traceId;

    /*
     * 接收消息的UID列表
     */
    private List<Long> idList;

    /*
     * 忽略的设备类型，不给这些终端推送消息
     */
    private final List<String> channelFilter = new LinkedList<>();

    private long id;
    private String action;
    private String title;
    private String content;
    private String sender;
    private String format;
    private String extra;
    private long timestamp;

    public String getTraceId() {
        return traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public String getExtra() {
        return extra;
    }

    public void setExtra(String extra) {
        this.extra = extra;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public void addExcludedChannel(String channel){
        if (channel != null){
            this.channelFilter.add(channel);
        }
    }

    public List<String> getChannelFilter() {
        return channelFilter;
    }

    public List<Long> getIdList() {
        return idList;
    }

    public void setIdList(List<Long> idList) {
        this.idList = idList;
    }

    public void setMessage(com.workcell.workdata.im.entity.Message message) {
        this.id = message.getId();
        this.action = message.getAction();
        this.title = message.getTitle();
        this.content = message.getContent();
        this.sender = message.getSender().toString();
        this.format = message.getFormat().toString();
        this.extra = message.getExtra();
        this.timestamp = message.getCreateTime().getTime();
    }



    /**
     * 返回构建好的 待发送消息列表
     * @return 消息列表
     */
    @JsonIgnore
    public List<Message> getMessageList(){
        return idList.stream().map(uid -> {
            Message message = new Message();
            message.setId(this.id);
            message.setSender(this.sender);
            message.setReceiver(uid.toString());
            message.setAction(this.action);
            message.setContent(this.content);
            message.setExtra(this.extra);
            message.setTitle(this.title);
            message.setFormat(this.format);
            message.setTimestamp(this.timestamp);
            return message;
        }).collect(Collectors.toList());
    }

    /**
     * 返回消息条件，满足条件才写入消息
     * @return
     */
    @JsonIgnore
    public Predicate<Channel> getPredicate(){
        return ChannelFilterPredicate.of(channelFilter);
    }
}
